<%@page language="java" contentType="text/html; charset=UTF-8"%>
<%@page import="java.util.*,com.qunar.common.admin.model.Resource,com.qunar.common.admin.util.ResourceNode" %>
<%@ taglib prefix="web" uri="http://meerkat.qunar.com/web"%>
<%!String loopTree(ResourceNode treeNode, int deep, int domId) {
        StringBuilder sb = new StringBuilder();
        if (treeNode != null) {
            sb.append(buildNodeStr(treeNode, deep * 20));
            for (ResourceNode node : treeNode.getChildren()) {
                if (node.getChildren() != null && node.getChildren().size() > 0) {
                    sb.append(loopTree(node, deep + 1, domId));
                } else {
                    sb.append(buildNodeStr(node, (deep + 1) * 20));
                }
            }
        }
        return sb.toString();
    }

    String buildNodeStr(ResourceNode node, int marginLeft) {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("<tr id=\"node_%s\">", node.getId()));
        sb.append("<td class=\"res_add\" style=\"padding-left:" + marginLeft + "px;\">");
        String type = "";
        switch (node.getType()) {
        case 0:
            type = "目录";
            break;
        case 1:
            type = "菜单项";
            break;
        case 2:
            type = "操作";
            break;
        }
        sb.append("├─ ");
        sb.append(node.getName()).append("&nbsp;&nbsp;");
        sb.append("<span>");
        sb.append("[" + type + "]");
        sb.append(" [" + node.getSort() + "]");
        sb.append("</span>");
        sb.append("<span class=\"addchildboard\" style=\"display:none;\">");
        if (node.getType() != 2) {
            sb.append(String.format("<a onclick=\"Qadmin.App.Resource.showAddResource('node_%s');\">增加节点</a>",
                    node.getId(), node.getId()));
        }
        sb.append(String.format("<a onclick=\"Qadmin.App.Resource.updateResource(%s);\">编辑节点</a>", node.getId()));
        sb.append(String.format("<a onclick=\"Qadmin.App.OptLog.showSingleResourceOptLog(%s);\">操作日志</a>", node.getId()));
        if (node.getChildren().size() == 0) {
            sb.append(String.format("<a onclick=\"Qadmin.App.Resource.deleteResource(%s);\">删除节点</a>", node.getId()));
        }
        sb.append("</span>");
        sb.append("</td>");
        sb.append("</tr>\r\n");
        return sb.toString();
    }%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<web:designCss/>
<%@include file="/WEB-INF/include/head.jsp" %>
<script src="${PATH}resources/js/fw/app/qadmin.app.resource.js"></script>
<script src="${PATH}resources/js/fw/app/qadmin.app.optlog.js"></script>
<style type="text/css">
.addchildboard a {
    cursor: pointer;
    margin: 3px;
    padding: 3px 5px;
    text-decoration:none;
}
.addchildboard a:hover {
    background: #F5F5F5;
}
</style>
</head>
<body>
    <div class="wrapper">
        <web:titleBar title="系统菜单" />

        <div class="main content">
        <table class="list" width="100%">
            <tr id="node_0">
                <td class="res_add">
                    根菜单
                    <span class="addchildboard" style="display:none;"><a onclick="Qadmin.App.Resource.showAddResource('node_0');">增加节点</a></span>
                </td>
            </tr>
            <%
                List<ResourceNode> nodeList = (List<ResourceNode>) request.getAttribute("nodeList");
                int deep = 1;
                for (ResourceNode node : nodeList) {
                    out.print(loopTree(node, deep, node.getId()));
                }
            %>
                <tr>
                    <td>
                        <input type="button" class="button button-blue" value="刷新" onclick="location.reload();"/>
                    </td>
                </tr>
        </table>
        </div>
    </div>
    <script type="text/javascript">
    $(document).ready(function() {
        $('.res_add').mouseover(function(){
            $(this).children(".addchildboard").show();
        }).mouseout(function() {
            $(this).children(".addchildboard").hide();
        });
    });
    </script>
</body>
</html>
